package com.pengtu.service.business;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.jbpm.api.task.Task;
import org.jbpm.pvm.internal.task.TaskImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.pengtu.Constants;
import com.pengtu.dao.Page;
import com.pengtu.dao.QueryFilter;
import com.pengtu.dao.business.AcctPetitionDao;
import com.pengtu.dao.business.AcctReportDao;
import com.pengtu.dao.business.FlowAcceptanceDao;
import com.pengtu.dao.quality.FlowProjectDao;
import com.pengtu.entity.app.User;
import com.pengtu.entity.business.acceptance.AcctPetition;
import com.pengtu.entity.business.acceptance.AcctReport;
import com.pengtu.entity.business.acceptance.FlowAcceptance;
import com.pengtu.entity.history.HistoryProject;
import com.pengtu.entity.project.ProjectInfo;
import com.pengtu.entity.quality.FlowProject;
import com.pengtu.service.BaseManager;
import com.pengtu.service.history.HistoryProjectManager;
import com.pengtu.service.project.ProjectManager;

/**
 * 交工质量验收检测报告相关实体的管理类
 *
 * @author liuyang
 *
 */
@Component
@Transactional
public class FlowAcceptanceManager extends BaseManager{
	private HistoryProjectManager historyProjectManager;
	private ProjectManager projectManager;
	private FlowAcceptanceDao flowAcceptanceDao;
	private AcctPetitionDao acctPetitionDao;
	private AcctReportDao acctReportDao;
	private FlowProjectDao flowProjectDao;

	@Autowired
	public void setHistoryProjectManager(
			HistoryProjectManager historyProjectManager) {
		this.historyProjectManager = historyProjectManager;
	}

	@Autowired
	public void setProjectManager(ProjectManager projectManager) {
		this.projectManager = projectManager;
	}

	@Autowired
	public void setFlowAcceptanceDao(FlowAcceptanceDao flowAcceptanceDao) {
		this.flowAcceptanceDao = flowAcceptanceDao;
	}

	@Autowired
	public void setAcctPetitionDao(AcctPetitionDao acctPetitionDao) {
		this.acctPetitionDao = acctPetitionDao;
	}

	@Autowired
	public void setAcctReportDao(AcctReportDao acctReportDao) {
		this.acctReportDao = acctReportDao;
	}
	
	@Autowired
	public void setFlowProjectDao(FlowProjectDao flowProjectDao) {
		this.flowProjectDao = flowProjectDao;
	}

	/**
	 * 保存
	 *
	 * @param flowAcceptance
	 */
	public void save(FlowAcceptance flowAcceptance) {
		flowAcceptanceDao.save(flowAcceptance);
	}
	
	public void save(FlowAcceptance flowAcceptance,
			HistoryProject historyProject) {
		flowAcceptanceDao.save(flowAcceptance);
		historyProject.setKey(flowAcceptance.getId());
		historyProjectManager.save(historyProject);
	}
	
	public void save(AcctPetition acctPetition) {
		acctPetitionDao.save(acctPetition);
	}
	
	/**
	 * 保存流程表和申请表
	 * save:
	 * 适用:
	 * @param acctPetition
	 * @param flowAcceptance 
	 * @exception 
	 * @since  1.0.0
	 */
	public void save(AcctPetition acctPetition,FlowAcceptance flowAcceptance) {
		flowAcceptanceDao.save(flowAcceptance);
		acctPetition.setFlowAcceptance(flowAcceptance);
		acctPetitionDao.save(acctPetition);
	}

	public void save(AcctReport acctReport) {
		acctReportDao.save(acctReport);
	}

	/**
	 * 根据ID查询
	 *
	 * @param id
	 * @return
	 */
	public FlowAcceptance getFlowAcceptance(String id) {
		return flowAcceptanceDao.get(id);
	}

	public AcctPetition getAcctPetition(String id) {
		return acctPetitionDao.get(id);
	}

	public AcctReport getAcctReport(String id) {
		return acctReportDao.get(id);
	}

	/**
	 * 根据项目ID查询
	 *
	 * @param projectInfo
	 * @return
	 */
	public List<FlowAcceptance> getFlowAcceptance(ProjectInfo projectInfo) {
		return flowAcceptanceDao.findBy("projectInfo", projectInfo);
	}

	public Page<FlowAcceptance> getFlowAcceptances(
			final Page<FlowAcceptance> page, final QueryFilter queryfilter) {
		return flowAcceptanceDao.findPage(page, queryfilter);
	}

	public AcctPetition getAcctPetition(FlowAcceptance flowAcceptance) {
		return acctPetitionDao.findUniqueBy("flowAcceptance", flowAcceptance);
	}

	public AcctReport getAcctReport(FlowAcceptance flowAcceptance) {
		return acctReportDao.findUniqueBy("flowAcceptance", flowAcceptance);
	}
	
	/**
	 * 待办任务
	 * findPersonalTasks:
	 * 适用:流程处理
	 * @param user
	 * @param projectInfo
	 * @return 
	 * @exception 
	 * @since  1.0.0
	 */
	public List<FlowAcceptance> findPersonalTasks(User user, ProjectInfo projectInfo) {
		List<Task> taskList = super.findPersonalTasks(user);
		List<FlowAcceptance> flowAcceptances = new ArrayList<FlowAcceptance>();
		for (Task task : taskList) {
			String taskId = task.getId();
			String arg = (String)((TaskImpl)task).getVariable(Constants.START_TYPE);
			if ("accept".equals(arg)) {
				String id = (String) super.getVariableByTaskId(taskId,
						Constants.ACCEPT_ID);
				FlowAcceptance flowAcceptance = flowAcceptanceDao.get(id);
				if (flowAcceptance != null) {
					if (StringUtils.equals(flowAcceptance.getProjectInfo()
							.getId(), projectInfo.getId())) {
						flowAcceptance.setTaskId(taskId);
						flowAcceptances.add(flowAcceptance);
					}
				}
			}
		}
		return flowAcceptances;
	}
	
	public FlowProject getByKey(String key) {
		return flowProjectDao.findUniqueBy("key", key);
	}
	
	/**
	 * 保存并启动流程
	 * 
	 * @param flowAcceptance
	 * @param deploymentId
	 * @param user
	 * @param historyProject
	 * @return
	 */
	public void start(FlowAcceptance flowAcceptance, String deploymentId,
			ProjectInfo projectInfo) {
		Map<String, Object> variables = new HashMap<String, Object>();
		variables.put(Constants.ACCEPT_ID, flowAcceptance.getId());
		super.start(flowAcceptance.getId(),deploymentId,
				 "accept",variables);
		flowAcceptanceDao.save(flowAcceptance);
		projectManager.saveOrupdateProj(projectInfo);
	}

	public String isFindPersonalTasks(User user, String id) {
		return super.isPersonalTasks(user, id, Constants.ACCEPT_ID);
	}
	
	public void takeTask(String taskId, String key, User user,
			HistoryProject historyProject) {
		super.takeTask(taskId, user);
		historyProject.setKey(key);
		historyProjectManager.save(historyProject);
	}
	
	public void completeTask(FlowAcceptance flowAcceptance, String outcome,
			Map<String, Object> variables, HistoryProject historyProject) {
		System.out.println("******************"+flowAcceptance.getTaskId());
		super.completeTask(flowAcceptance.getTaskId(), outcome, variables);
		historyProjectManager.save(historyProject);
		flowAcceptanceDao.save(flowAcceptance);
	}
}
